package com.FH.login;

import com.FH.util.AccessDB;
import com.FH.util.InternalInterface;
import com.FH.util.ResultDTO;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/13.
 */
@Stateless
public class LoginDAOImpl extends AccessDB implements LoginDAO {
    /**
     * 登录
     * @param member_name
     * @param member_password
     * @return
     */
    @Override
    public ResultDTO getLoginInfo(String member_name, String member_password,String orgId) throws Exception {
//        String existName = "0";
//
//        String syssql = "select sys_conf_id,sys_conf_name,sys_url_link,sys_url_username,create_date,sys_token,sys_conf_description,sys_role_url" +
//                "from systemconf";
//
//        List<String []> seresult = new ArrayList<String []>();
//
        List<LoginShowDTO> result = new ArrayList<LoginShowDTO>();
//
////        seresult = getReportingTo(syssql);
        String logininfo = "";
        JSONObject strJson = new JSONObject();

//        String str = InternalInterface.getHttp("http://127.0.0.1:8080/rest/userService/getUserOrgList?loginId="+member_name);
//        str = str.replace("[", "").replace("]", "");
//        if(null!= str && !"".equals(str)) {
//            JSONObject obj = new JSONObject(str);
//            String orgId = obj.getString("id");
        JSONObject login = new JSONObject();
        login.put("orgId", orgId);
        login.put("loginId", member_name);
        login.put("passWord", member_password);
        logininfo = InternalInterface.postHttp("http://127.0.0.1:8088/rest/userService/login", login.toString());

        strJson = new JSONObject(logininfo);
//        }

//        for (int i = 0; i < seresult.size(); i++){
//
//            if(null !=seresult.get(i)[8] && !seresult.get(i)[8].equals("")){
//                String str = InternalInterface.getHttp(seresult.get(i)[8]+member_name);
//                str = str.replace("[", "").replace("]", "");
//                JSONObject obj = new JSONObject(str);
//                String orgId = obj.getString("id");
//                JSONObject login = new JSONObject();
//                login.put("orgId", orgId);
//                login.put("loginId", member_name);
//                login.put("passWord", member_password);
//                String logininfo = InternalInterface.postHttp(seresult.get(i)[8], login.toString());
//
//                JSONObject jsonObject = new JSONObject(logininfo);
//
//                jsonArray.put(jsonObject);
//            }
//            else{
//                JSONObject obj = new JSONObject();
//                obj.put("loginId", member_name);
//                obj.put("passWord", member_password);
//                String logininfo = InternalInterface.postHttp(seresult.get(i)[8], obj.toString());
//
//                JSONObject jsonObject = new JSONObject(logininfo);
//
//                jsonArray.put(jsonObject);
//            }
//
//        }



        ResultDTO resultDTO = new ResultDTO();

        if ((null!= logininfo && !"".equals(logininfo))) {
            if(Integer.parseInt(strJson.get("status").toString()) != -2) {
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion("用户信息调用成功");
                resultDTO.setLoginstr(logininfo);
            }
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("用户名密码错误");
        }

        resultDTO.setValues(result);
        System.out.println(resultDTO);

     /*         }catch (IOException e){
        e.getMessage();
        }*/
        return resultDTO;
    }

    @Override
    public ResultDTO georgInfo(String member_name) throws Exception {
        String str = InternalInterface.getHttp("http://127.0.0.1:8088/rest/userService/getUserOrgList?loginId="+member_name);
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setStr(str);
        return resultDTO;
    }



    /**
     * oa
     * @return
     * @throws Exception
     */
    @Override
    public ResultDTO updateOALoginInfo() throws Exception {
//        String[] loginCookie =  CookieUtil.getCookies();
//         if(loginCookie!=null&&loginCookie.length>0){
//            for(int i=0;i<loginCookie.length;i++){
//                String member_name = loginCookie[0];
//                String member_password = loginCookie[1];
//                if(!"".equals(member_name)){
//                    //调注册操作oa系统
//                    KamMemberPullDTO kamMemberPullDTO = new KamMemberPullDTO();
//                    kamMemberPullDTO.setMember_name(member_name);
//                    kamMemberPullDTO.setMember_password(member_password);
//                    kamMemberResource.getLoginInfo(kamMemberPullDTO.getMember_name(),kamMemberPullDTO.getMember_password());
//
////                    ExtInterfaceCall.postHttp("http://127.0.0.1:8080/officemanager/",
////                    new Oauth().changeToJson(member_name,member_password).toString());
////                    String sql = "select count(1) from login where member_name='"+member_name+"' and member_password='"+member_password+"'";
////                    List<String[]> seresult = new ArrayList<String[]>();
////                    seresult=this.getReportingTo(sql);
//                    }//else(){
//                    //调登录接口
//                }
//
//            }
        return null;
    }

    /**
     * 题库
     * @return
     * @throws Exception
     */
    @Override
    public ResultDTO updateTiKuLoginInfo() throws Exception {
        return null;
    }

    @Override
    public ResultDTO updateCePingLoginInfo() throws Exception {
        return null;
    }

    @Override
    public ResultDTO updateBaseLoginInfo() throws Exception {
        return null;
    }
}
